<!DOCTYPE html>
<html>

<head>
    <title>wnr</title>
    <meta charset="UTF-8" />
    <meta http-equiv="Content-Type" content="text/html" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimal-ui" />
    <script>
        window.$ = require("jquery/dist/jquery.slim.min.js");
        require("bootstrap/dist/js/bootstrap.min.js");
    </script>
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="res/fonts/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body style="position: fixed">
    <!-- makes the window resizable -->
    <div class="header-resize"></div>
    <div class="left-resize"></div>
    <div class="right-resize"></div>
    <div class="bottom-resize"></div>

    <script src="supporter.js"></script>
    <div class="d-flex mx-auto justify-content-center align-items-center text-dark" id="main">
        <div id="time-bar" class="text-muted"></div>
        <div id="time-bar-macos" class="text-muted"></div><!-- time-bar for systems -->
        <div id="controller">
            
            <a href="javascript:floatingOpener()" class="small work" id="mini-mode"><i class="iconfont icon-suoxiao"
            ></i></a>&nbsp;&nbsp;
            <a href="javascript:call('window-hide')" class="small work" id="win-hider"><i
                    class="iconfont icon-dockpanel" id="window-hide"></i></a><span
                id="no-use-space">&nbsp;&nbsp;</span>
            <a href="javascript:call('window-maximize')" class="small work" id="win-max"><i
                    class="iconfont icon-maximize" id="window-max"></i></a>&nbsp;&nbsp;
            <a href="javascript:call('window-minimize')" class="small work" id="window-minimize"><i
                    class="iconfont icon-minus"
            ></i></a><span
                class="should-lock">&nbsp;&nbsp;</span>
            <a href="javascript:call('window-close-chk')" class="small work should-lock"
               id="exit"><i
                    class="iconfont icon-close"></i></a>
            <script>
                $('#helper').attr('title', i18n.__('helper'));
              //  $('#add-time').attr('title', i18n.__('minus-mode'));
                $('#mini-mode').attr('title', i18n.__('mini-mode-msg'));
                $('#window-hide').attr('title', i18n.__('window-hide'))
                $('#window-max').attr('title', i18n.__('window-max'));
                $('#window-minimize').attr('title', i18n.__('window-minimize'));
                $('#exit').attr('title', i18n.__('exit'));
            </script>
        </div>
        <div id="timer" class="justify-content-center align-content-center text-center">
            &nbsp;&nbsp;
            <input type="text" id="title" class="text-center h4" maxlength="13"
                   style="transform: translateX(5px); font-size: 0.4rem"
                   oninput="modifyExternalTitleWin()" /><a href="javascript:openExternalTitleWin()"
                                                           id="external" class="work"><i
                class="iconfont icon-external-link"></i></a><br />
            <div contenteditable="true" id="notes" class="text-center small text-muted"
                 style="font-size: 0.4rem; margin-bottom: 4px"
                 oninput="modifyExternalTitleWin()"></div>
            <script>
                function openExternalTitleWin() {
                    ipc.send("open-external-title-win", { title: $("#title").val(), notes: $("#notes").text() });
                }

                function modifyExternalTitleWin() {
                    ipc.send("modify-external-title-win", { title: $("#title").val(), notes: $("#notes").text() });
                }

                ipc.on("sync-title", function (event, message) {
                    $("#title").val(message.title);
                    $("#notes").text(message.notes);

                    titleNodeResizer();
                    notesNodeResizer();
                });

                $("#external").attr('title', i18n.__('external-title'));
                $("#title").attr('title', i18n.__('title-edit-msg'));
                $("#notes").attr('title', i18n.__('notes-edit-msg'));

                $("#external").css("opacity", "0");

                let titleNode = document.getElementById("title");
                titleNode.addEventListener('input', titleNodeResizer);

                function titleNodeResizer() {
                    let n = document.createElement('h4');
                    n.style.display = "inline-block"
                    n.style.whiteSpace = "pre"
                    n.style.maxWidth = "90%"
                    n.innerHTML = titleNode.value;
                    titleNode.parentNode.insertBefore(n, titleNode.nextSibling);
                    let spanW = n.getBoundingClientRect().width;
                    if (spanW < 10) {
                        spanW = 192;
                        $("#external").css("opacity", "0");
                        $("#title").css("font-size", "0.4rem");
                    } else {
                        $("#external").css("opacity", "1");
                        $("#title").css("font-size", "1rem");
                    }
                    titleNode.parentNode.removeChild(n);
                    titleNode.style.width = spanW + 'px'
                }

                let notesNode = document.getElementById("notes");

                // limit content-editable div (used for multi-lines notes) length
                notesNode.addEventListener('keydown', function (e) {
                    setTimeout(function () {
                        let allowedKeys = (
                                e.which === 8 ||  /* BACKSPACE */
                                e.which === 35 || /* END */
                                e.which === 36 || /* HOME */
                                e.which === 37 || /* LEFT */
                                e.which === 38 || /* UP */
                                e.which === 39 || /* RIGHT*/
                                e.which === 40 || /* DOWN */
                                e.which === 46 || /* DEL*/
                                e.ctrlKey === true && e.which === 65 || /* CTRL + A */
                                e.ctrlKey === true && e.which === 88 || /* CTRL + X */
                                e.ctrlKey === true && e.which === 67 || /* CTRL + C */
                                e.ctrlKey === true && e.which === 86 || /* CTRL + V */
                                e.ctrlKey === true && e.which === 90    /* CTRL + Z */
                        );
                        if (!allowedKeys && $(e.target).text().length >= 72) {
                            e.preventDefault();
                            if ($(e.target).text().length >= 72)
                                $(e.target).text($(e.target).text().slice(0, 72));
                        }
                        notesNodeResizer();
                    });
                });
                notesNode.addEventListener('paste', function (e) {
                    setTimeout(function () {
                        $(e.target).text($(e.target).text().slice(0, 72));
                        notesNodeResizer();
                    });
                    if ($(e.target).text().length >= 72) e.preventDefault();
                });

                function notesNodeResizer() {
                    let n = document.createElement('span');
                    n.style.display = "inline-block"
                    n.style.whiteSpace = "pre"
                    n.style.maxWidth = "90%"
                    n.innerHTML = notesNode.innerText;
                    notesNode.parentNode.insertBefore(n, notesNode.nextSibling);
                    let spanW = n.getBoundingClientRect().width;
                    if (notesNode.innerText.length === 0) {
                        spanW = 300;
                        $("#notes-space").css("display", "none");
                        $("#notes").css("font-size", "0.4rem");
                    } else {
                        $("#notes-space").css("display", "block");
                        $("#notes").css("font-size", "0.7rem");
                    }
                    notesNode.parentNode.removeChild(n);
                    notesNode.style.width = spanW > 300 ? 300 : spanW + 'px'
                }
            </script>
            <br />
            <span id="time-cnt" class="text-center small text-muted"></span>
            <span id="infinity-label" class="text-center small text-muted">
                <script>document.write(i18n.__('infinity'));</script>
            </span>
            <span id="stop-count" class="text-center small text-muted"></span>
            <span id="time-cnt-blank"><br /><br /></span>
            <div id="work-rest" class="text-center work">
                <script>document.write(i18n.__('working'));</script>
            </div>
            <br />
            <div id="now-timing" class="text-center h1 work"></div>
            <br />
            <div class="text-center"><a id="stopper" href="javascript:stopAndCount()" class="text-muted"><i
                    class="iconfont icon-pause"></i></a></div>
            <br />
            <div class="text-center text-muted" id="more-options">
                <a href="javascript:backcheck()" class="text-muted" id="back-index"><i class="iconfont icon-previous"
                ></i><span
                        class="focuser extreme-small controller-msg">
                    <script>document.write(i18n.__('back'));</script>
                </span></a>
                <span id="skipper-space" class="should-lock">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                <a href="javascript:skipper()" class="text-muted" id="skipper-container">
                    <span
                            class="focuser extreme-small controller-msg">
                    <script>document.write(i18n.__('skip'));</script>
                </span><i class="iconfont icon-fastforward" id="skipper"></i>
                </a>
                <br />
            </div>
            <input name="passcode-locker" id="passcode-rechecker" type="password" maxlength="11"
                   onkeydown="if(event.keyCode === 13) recheck($('#passcode-rechecker').val());" />
            <br />
            <div class="text-center lead" id="backer">
                <a href="javascript:backcheck()" class="text-muted" id="backindex2"><i class="iconfont icon-previous"
                ></i></a>
            </div>
            <div class="text-center text-muted" id="only-one">
                <h3>
                    <script>document.write(i18n.__('only-one-min-left'));</script>
                </h3>
            </div>
            <div id="fullscreen-experience-tip" class="small font-weight-bold text-muted text-center">
                <script>document.write(i18n.__('fullscreen-experience-tip') +
                        '<br /><a href="javascript:mistakeResolver()" class="rest">' +
                        i18n.__('fullscreen-experience-tip-mistake') + '</a>');</script>
            </div>
            <!-- container -->
            <div class="dropdown">
                <button title="Time Adjustment" class="btn btn-secondary dropdown-toggle btn-sm" type="button" id="time-adjust-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <script>document.write(i18n.__('time_adjustment'));</script>
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <!-- decrease5minutes -->
                    <a class="dropdown-item" href="javascript:adjustTime(-5)">
                        <i class="iconfont icon-previous"></i> <script>document.write(i18n.__('decrease-5-minutes'));</script>
                      </a>
                    <!-- increase5minutes -->
                    <a class="dropdown-item" href="javascript:adjustTime(5)" >
                        <i class="iconfont icon-fastforward"></i> <script>document.write(i18n.__('increase-5-minutes'));</script>
                      </a>
                    <div class="dropdown-divider"></div>

                    <!-- decrease1minutes -->
                    <a class="dropdown-item" href="javascript:adjustTime(-1)" >
                        <i class="iconfont icon-previous"></i> <script>document.write(i18n.__('decrease-1-minute'));</script>
                    </a>
                    <!-- increase1minutes -->
                    <a class="dropdown-item" href="javascript:adjustTime(1)" >
                        <i class="iconfont icon-fastforward"></i> <script>document.write(i18n.__('increase-1-minute'));</script>
                    </a>
                    <div class="dropdown-divider"></div>

                    <!-- decrease15seconds -->
                    <a class="dropdown-item" href="javascript:adjustTime(-0.25)" >
                        <i class="iconfont icon-previous"></i> <script>document.write(i18n.__('decrease-15-seconds'));</script>
                    </a>
                    <!-- increase15seconds -->
                    <a class="dropdown-item" href="javascript:adjustTime(0.25)">
                        <i class="iconfont icon-fastforward"></i> <script>document.write(i18n.__('increase-15-seconds'));</script>
                    </a>
                    <div class="dropdown-divider"></div>

          
                    <a class="dropdown-item" href="javascript:mistake()" id="mistake-button">
                        <script>document.write(i18n.__('fullscreen-experience-tip-mistake'));</script>
                    </a>
                </div>
            </div>
            <script>       
                $(document).ready(function() {
                    // For method 3, show time adjustment by default, but respect user preference
                    if (methodFromStart === 3) {
                        if (isFeatureDisabled(store.get('disable-time-adjust'), 1)) {
                            $("#time-adjust-dropdown").hide();
                        } else {
                            $("#time-adjust-dropdown").show();
                        }
                    }
                    
                    // Add CSS for disabled state
                    $("<style>")
                        .prop("type", "text/css")
                        .html(`
                            .dropdown-item.disabled {
                                color: #6c757d !important;
                                cursor: not-allowed !important;
                            }
                            .dropdown-item.disabled:hover {
                                background-color: transparent !important;
                            }
                        `)
                        .appendTo("head");
                });
            </script>

            <div id="fullscreen-too-long-tip" class="small font-weight-bold text-muted text-center">
                <script>document.write(i18n.__('fullscreen-too-long-tip') +
                        '<br /><a href="javascript:mistakeResolver()" class="rest">' +
                        i18n.__('fullscreen-experience-tip-mistake') + '</a>');</script>
            </div>
            <script>
                $('#passcode-rechecker').attr('placeholder', i18n.__('quit-from-timer-in-lock-mode-msg'));
                $('#back-index').attr('title', i18n.__('back-index'));
                $('#backindex2').attr('title', i18n.__('back-index'));
                $('#stopper').attr('title', i18n.__('start-or-stop'))
                $('#backer').css('display', 'none');
                $('#skipper-container').attr('title', i18n.__('skipper'));
                if (process.platform === "darwin") $("#controller").css("display", "none");
            </script>
        </div>
    </div><!-- for things with 'work' mark, when it's resting, the color should be changed -->
    <div id="bottom-bar">&nbsp;</div>

    <div id="fullscreen-custom-dialog" class="justify-content-left align-content-left text-left d-none">
        <p class="small text-grey font-weight-bolder" style="margin-left: 0" id="dialog-title">wnr</p>
        <p id="dialog-msg" class="small" style="line-height: 1.5; width: 210px; line-break: anywhere">Message</p>
        <div id="dialog-buttons" style="width:210px" class="text-right">
            <a type="button" id="dialog-ok" class="btn btn-outline-primary btn-sm text-right"
               style="font-size: 12px"
               href="javascript:fullscreenCustomDialog('off', '', '', '')">
                <script>document.write(i18n.__('ok'))</script>
            </a>
        </div>
    </div>

    <script>
        var executeAfterRecorder = "";

        function fullscreenCustomDialog(mode, title, msg, executeAfter) {
            if (mode === "on") {
                $("#fullscreen-custom-dialog").removeClass("d-none");
                $("#dialog-title").text(title);
                $("#dialog-msg").text(msg);
                executeAfterRecorder = executeAfter;
            } else {
                $("#fullscreen-custom-dialog").addClass("d-none");
                ipc.send("eval", executeAfterRecorder);
            }
        }

        ipc.on("fullscreen-custom-dialog", function (event, msg) {
            fullscreenCustomDialog("on", msg.title, msg.message, msg.executeAfter);
        })
    </script>

    <script>
        function dynamicResizer() {//dynamically resize the countdown text as the window resizes
            let hdWidth = document.body.getBoundingClientRect().width;
            let defaultWidth = 376;
            let scale = hdWidth / defaultWidth;

            $("#now-timing").css("font-size", (2.5 * ((scale - 1) / 2 + 1)) + 'rem');
            if (hdWidth > 630) {
                $("#now-timing").css("font-family", "SourceHanSansCNLight, sans-serif");
                if (hdWidth < 800) $("#now-timing").css("font-weight", "bolder");
                else $("#now-timing").css("font-weight", "normal");
            } else {
                $("#now-timing").css("font-family", "SourceHanSansCN, sans-serif");
                $("#now-timing").css("font-weight", "normal");
            }
        }

        dynamicResizer();
        window.onresize = dynamicResizer;
    </script>

    <script>
        store.set("just-back", false);
        if (process.platform === "darwin") {
            $("#win-hider").css("display", "none");
            $("#no-use-space").css("display", "none");
        }
        $("#only-one").css("display", "none");
        $("#passcode-rechecker").css("display", "none");
        store.set('just-launched', false);

        if (store.has("timer-time")) {
            if (!store.get("timer-time")) {
                $("#time-bar-macos").css("opacity", 0);
                $("#time-bar").css("opacity", 0);
            }
        }

        hasFloating = false;
        canOpenFloating = true;

        function floatingOpener() {
            if (canOpenFloating) {
                call("window-hide");
                call("floating");
                hasFloating = true;

                //repeat so always able to be sent
                if (methodFromStart !== 3)
                    for (let floatingRepeater = 1; floatingRepeater < 5; floatingRepeater++)
                        setTimeout(function () {
                            ipc.send("floating-conversation", {
                                topic: "time-left",
                                val: (((method === 1) ? workTime : restTime) - new Date().getTime() + startTime -
                                        (times === 0 ? firstTimeDiscount : 0)) / 60000,
                                percentage: Math.round(lastRecordedProgress * 100),
                                method: method,
                                isWorking: isClockWorking
                            });
                        }, 500 * floatingRepeater);
                else
                    for (let floatingRepeater = 1; floatingRepeater < 5; floatingRepeater++)
                        setTimeout(function () {
                            ipc.send("floating-conversation", {
                                topic: "time-left",
                                val: seconds / 60,
                                percentage: 0,
                                method: 3,
                                isWorking: isClockWorking
                            })
                        }, 500 * floatingRepeater);
            }
        }

        //for first time users
        function mistakeResolver() {
            if (store.get("islocked")) $("#passcode-rechecker").css("display", "inline");
            else backRelauncher();
        }
        function mistake(){
            if (hasSoonFinishTipPosted) return; // ban stopping after tip posted
            
            // Check if canceling is disabled based on current method and preferences
            if (isFeatureDisabled(store.get('disable-backing'), method)) {
                ipc.send("alert", i18n.__("fail-to-withdraw"));
                return;
            }
            
            // If we can cancel, go back
            if (methodFromStart === 3 || ((!store.get("islocked")) && (!isFeatureDisabled(store.get('disable-backing'), method)))) {
                backcheck();
            } else {
                ipc.send("alert", i18n.__("fail-to-withdraw"));
            }
        }

        $("#fullscreen-experience-tip").css("display", "none");

        //lock mode settings
        function recheck(passcode) {
            let md5 = require('crypto-js/md5');
            if (passcode === "") ipc.send("alert", i18n.__('locker-settings-empty-password'));
            else if (passcode === store.get('lockerpasscode') || md5(passcode).toString() === store.get('lockerpasscode')) backRelauncher();
            else ipc.send("alert", i18n.__('locker-settings-input-tip-wrong-password'));
        }

        function backcheck() {
            if (hasSoonFinishTipPosted) return;// ban stopping after tip posted
            if (store.get("islocked")) $("#passcode-rechecker").css("display", "inline");
            else backer();
        }

        function backRelauncher() {
            call('floating-destroy');
            store.set("just-back", true);
            timingData.set("last-recorded-minutes-left", -1);
            timingData.set("last-recorded-hours-left", -1);
            store.set("fullscreen-experience", true);
            if (!isClockWorking) {
                stopper();
                setTimeout(function () {
                    call('relauncher');
                }, 500);
            } else call('relauncher');
        }

        function backer() {
            call('floating-destroy');
            call('exit-fullscreen');
            store.set("just-back", true);
            timingData.set("last-recorded-minutes-left", -1);
            timingData.set("last-recorded-hours-left", -1);
            if (!isClockWorking) {
                try {
                    stopper();
                } catch {
                }
                setTimeout(function () {
                    window.location.href = "index.html";
                }, 500);
            } else window.location.href = "index.html";
        }

        function keydownOnError(e) {
            if (e.keyCode === 13) backer();
        }

        function longBreakGetter() {
            if (store.has("long-break")) {
                let choices = [0, 3, 5, 7, 10, 15, 20];
                return choices[store.get("long-break")];
            } else return 0;
        }

        let $_GET = (function () {
            let url = window.document.location.href.toString();
            let u = url.split("?");
            if (typeof (u[1]) == "string") {
                u = u[1].split("&");
                let get = {};
                for (let i in u) {
                    let j = u[i].split("=");
                    get[j[0]] = j[1];
                }
                return get;
            } else {
                return {};
            }
        })();

        var
                title = decodeURI($_GET['title']),
                note = decodeURI($_GET['note']),
                workTime = decodeURI($_GET['work-time']) * 60000,
                restTime = decodeURI($_GET['rest-time']) * 60000,//use ms
                originalRestTime = restTime,
                originalWorkTime = workTime,
                loop = Number(decodeURI($_GET['loop'])),
                workTimeFocused = (decodeURI($_GET['work-time-focused']) !== "false"),
                restTimeFocused = (decodeURI($_GET['rest-time-focused']) !== "false"),
                restored = (decodeURI($_GET['restored']) !== "false"),
                times = (isNaN(Number(decodeURI($_GET['times'])))) ? 0 : Number(decodeURI($_GET['times'])),
                methodFromStart = Number(decodeURI($_GET['method'])),
                continueLoop = (decodeURI($_GET['continueloop']) !== "false"),
                h = i18n.__('h'),
                min = i18n.__('min'),
                s = i18n.__('s'),
                stopperTimeout = null,
                pausingTimeout = null,
                isLongRest = false,
                isFirstTimeOnlyRest = decodeURI($_GET['work-time']) === "0.7777",
                isOnlyRest = isFirstTimeOnlyRest,
                firstTimeDiscount = isNaN(decodeURI($_GET['discount'])) ? 0 : decodeURI($_GET['discount']) * 60000,
                longBreakFrequence = -1;

        if (isOnlyRest) ipc.send("enter-only-rest");

        if (store.has("long-break-mode-alter")) {
            let frequenceList = [-1, 2, 3, 4, 5, 6];
            longBreakFrequence = frequenceList[store.get("long-break-mode-alter")];
        }

        ipc.send('focus-mode-settings', { workTimeFocused: workTimeFocused, restTimeFocused: restTimeFocused });

        if (restored && methodFromStart === 2) times--;//workaround: method = 2 causes an extra skipper()

        timingData.set("last-recorded-state", {
            workTime: workTime / 60000,
            restTime: restTime / 60000,
            loop: loop,
            title: title,
            note: note,
            isWorkTimeFocused: workTimeFocused,
            isRestTimeFocused: restTimeFocused,
            method: methodFromStart
        });

        let $nowTiming = $("#now-timing"), $bottomBar = $("#bottom-bar"), $timeBar = $("#time-bar"),
                $timeBarMacOS = $("#time-bar-macos");//cache frequently using doms

        //method = 3 means liberal / positive timer
        if (methodFromStart === 3) {
            $(".work").addClass("positive");
            $("#bottom-bar").css("display", "none");

            timingData.set("last-recorded-minutes-left", 0);
            timingData.set("last-recorded-hours-left", 0);

            ipc.send("enter-positive-timing");
            
            // In liberal mode, mistake button should always be enabled
            $("#mistake-button").removeClass("disabled").css("pointer-events", "auto").css("opacity", "1");
            
            // In liberal mode, time adjustment should always be available
            $("#time-adjust-dropdown").show();
        }

        if (isFeatureDisabled(store.get('disable-pausing'), methodFromStart)) {
            $("#stopper").css("display", "none");
        }
        if (isFeatureDisabled(store.get('disable-skipping'), methodFromStart)) {
            $("#skipper-container").css("display", "none");
            $("#skipper-space").css("display", "none");
        }
        // special dealing for enabling back for infinity mode and liberal timing
        if (isFeatureDisabled(store.get('disable-backing'), methodFromStart) && methodFromStart !== 3 && loop !== 0) {
            $("#back-index").css("display", "none");
            $("#skipper-space").css("display", "none");
        }
        
        // Helper function to check if a feature should be disabled
        function isFeatureDisabled(settingValue, currentMethod) {
            if (settingValue === 0) return true; // Always disabled
            if (settingValue === 1 && currentMethod === 1) return true; // Disabled when working
            if (settingValue === 2 && currentMethod === 2) return true; // Disabled when resting
            return false; // Never disabled (value === 3)
        }
        
        // Set initial state for mistake button based on preferences
        const disableBackingValue = store.get('disable-backing');
        if (isFeatureDisabled(disableBackingValue, methodFromStart)) {
            $("#mistake-button").addClass("disabled").css("pointer-events", "none").css("opacity", "0.5");
        }
        
        // Set initial state for time adjustment dropdown based on preferences and focused mode
        const disableTimeAdjustValue = store.get('disable-time-adjust');
        const isFocusedMode = (methodFromStart === 1 && workTimeFocused) || (methodFromStart === 2 && restTimeFocused);
        const isLooseMode = methodFromStart === 3;
        
        if (isFeatureDisabled(disableTimeAdjustValue, methodFromStart) || (isFocusedMode && !isLooseMode)) {
            $("#time-adjust-dropdown").hide();
        }

        if ((loop !== Math.floor(loop)) || (loop < 1 && loop !== 0) || (workTime < 5000) || (restTime < 5000) || isNaN(workTime) || isNaN(restTime) || isNaN(loop)) {//use ms for 1s
            timingData.set("last-recorded-minutes-left", 0);
            timingData.set("last-recorded-hours-left", 0);
            $("#timer").html("<p class='text-center text-muted lead' id='timer-error'>" + i18n.__('not-enough') + "<br/><a href='javascript:backer()' class='work'>" + i18n.__('back') + "</a></p>");
            $("#timer").css("width", "75%");
            isTimerWindow(false);
            document.onkeydown = keydownOnError;
        } else if (workTime > 86400000 || restTime > 86400000 || (workTime + restTime) * loop > 86400000) {
            timingData.set("last-recorded-minutes-left", 0);
            timingData.set("last-recorded-hours-left", 0);
            $("#timer").html("<p class='text-center text-muted lead' id='timer-error'>" + i18n.__('too-long') + "<br/><a href='javascript:backer()' class='work'>" + i18n.__('back') + "</a></p>");
            $("#timer").css("width", "75%");
            isTimerWindow(false);
            document.onkeydown = keydownOnError;
        } else {
            isTimerWindow(true);
            $bottomBar.css("background-color", newWorkColor + "99");
            if (title) {
                $("#title").val(title);
                titleNodeResizer();
            }
            if (note) {
                $("#notes").text(note);
                notesNodeResizer();
            }
            if (loop === 0 || method === 3 || continueLoop) {
                $("#time-cnt").css("display", "none");
                if (continueLoop || method === 3) {
                    $("#infinity-label").css("display", "none");
                    if (!continueLoop) $("#time-cnt-blank").css("display", "none");
                }
            } else {
                $("#infinity-label").css("display", "none");
                $("#time-cnt").text(Math.ceil((times + 1) / 2) + ' / ' + loop + ' ' + i18n.__('times'));
            }
            $("#fullscreen-too-long-tip").css("display", "none");
            if (workTimeFocused) {
                if (styleCache.get("is-shadowless") !== true) $("html").css("border", "none");
                $bottomBar.css("background-color", newWorkColor + "19");
                $bottomBar.css("height", "100%");
                ipc.send('warning-giver-restend');
                $("#controller").css("display", "none");
                $("#more-options").css("display", "none");
                $timeBar.addClass("time-bar-focus");
                $timeBarMacOS.addClass("time-bar-focus");
                $nowTiming.addClass("display-1");
                $nowTiming.removeClass("h1");
                if (!store.get("no-check-time-end")) stopperTimeout = setTimeout(stopper, 500);
                ipc.once('warning-closed', function () {
                    stopper();
                });
                if (store.get("fullscreen-experience") !== true) {
                    $("#fullscreen-experience-tip").css("display", "inline-block");
                    store.set("fullscreen-experience", true);
                } else if (workTime >= 600000 && restored === false) {
                    $("#fullscreen-too-long-tip").css("display", "block");
                    setTimeout(function () {
                        $("#fullscreen-too-long-tip").css("display", "none");
                    }, 12000);
                }
            }

            //method: 1-work / 2-rest, moreThanOneMin: more than 1 min left
            var startTime,
                    int,
                    pauseInt = null,
                    tempVariable,
                    nowTime,
                    timeGot,
                    seconds,
                    minutes,
                    hours,
                    method = 1,
                    isClockWorking = 1,
                    moreThanOneMin = ((method === 1 && workTime > 60000) || isOnlyRest && restTime > 60000) ? 1 : 0,
                    lastRecordedMinutesLeft = -1,
                    lastRecordedHoursLeft = -1,
                    systemName = process.platform,
                    progress = 0.00,
                    remainMinutes = 0,
                    lastRecordedRemain = -1,
                    lastRecordedSecond = -1,
                    lastRecordedProgress = 0.00,
                    wasCounting = true,
                    shouldNap,
                    napInterval = 0,
                    lastNapSecond = 0,
                    canStop = true,
                    stopCounter = 0,
                    isBothSkipperAndBackerEnabled = true,
                    soonFinishTipMethod = 0,
                    hasSoonFinishTipPosted = false,
                    lastTimePositiveMinutes = -1;

            if (store.has("soon-finish-tip")) {
                soonFinishTipMethodList = [0, 5, 7, 10, 15, 20];
                soonFinishTipMethod = soonFinishTipMethodList[store.get("soon-finish-tip")];
            }

            if (store.get("nap") || methodFromStart === 3) {
                if (methodFromStart === 3) method = 3;
                shouldNap = true;
                napInterval = (store.has("nap-time") ? store.get("nap-time") : 10) * 60;
                if (isNaN(napInterval)) napInterval = 600;
                if (methodFromStart !== 3) lastNapSecond = workTime / 1000;
                else lastNapSecond = 0;
            } else shouldNap = false;

            if (methodFromStart !== 3) {
                startTime = new Date().getTime();
                int = setInterval(clock, 250);
            } else {
                startTime = new Date().getTime();
                int = setInterval(positive, 250);
                $("#infinity-label").css("display", "none");
            }

            if (methodFromStart === 2) skipper();

            ipc.on('start-or-stop', function () {
                if (hasSoonFinishTipPosted) return;// ban stopping after tip posted
                if (canStop) {
                                                    if (isFeatureDisabled(store.get("disable-pausing"), method)) {
                    ipc.send("alert", i18n.__('pausing-disabled'));
                } else {
                    stopper();
                    ipc.send("floating-conversation", { topic: "stop-sync", val: isClockWorking });
                }
                }
            });

            ipc.on('alter-start-stop', function (event, message) {
                if (message === "start") {
                    if ((!isClockWorking) && (wasCounting)) {
                        stopper();
                        ipc.send("floating-conversation", { topic: "stop-sync", val: isClockWorking });
                        wasCounting = !wasCounting;
                    }
                } else if (message === "stop") {
                    if (isClockWorking && wasCounting) {
                        stopper();
                        ipc.send("floating-conversation", { topic: "stop-sync", val: isClockWorking });
                        wasCounting = !wasCounting;
                    }
                }
            });

            ipc.on('remote-control-msg', function (event, message) {
                if (hasSoonFinishTipPosted) return;// ban stopping after tip posted

                if (message === "skipper") skipper();
                else if (message === "back") {
                    if (methodFromStart === 3 || ((!store.get("islocked")) && (!isFeatureDisabled(store.get('disable-backing'), method))))
                        backcheck();
                    else
                        ipc.send("alert", i18n.__("fail-to-withdraw"));
                } else if (message === "stop") stopper();
                else if (message === "enter") floatingOpener();
                else if (message === "closed") hasFloating = false;
            });

            function stopAndCount() {
                if (hasSoonFinishTipPosted) return;// ban stopping after tip posted

                stopper();
                if (!wasCounting) stopCounter++;
                if (stopCounter) $("#stop-count").text(((methodFromStart === 3 || continueLoop) ? "" : " | ") + i18n.__("already-paused") + " " + stopCounter.toString() + " " + i18n.__("time(s)"));
            }

            ipc.on("can-redo-exec", function () {
                window.clearInterval(int);
                startTime = new Date().getTime();
                if (methodFromStart !== 3)
                    int = setInterval(clock, 250);
                else {
                    $("#infinity-label").css("display", "none");
                    int = setInterval(positive, 250);
                }
                $("#stopper").html("<i class='iconfont icon-pause' aria-hidden='true'></i>");
                isClockWorking = 1;//to restart
                ipc.send("tray-image-change", "start");
                if (pausingTimeout) window.clearInterval(pausingTimeout);
            });

            function stopper() {
                wasCounting = !wasCounting;
                if (isClockWorking) {
                    window.clearInterval(int);
                    pauseInt = setTimeout(function () {
                        ipc.send("can-redo-alert");
                    }, 1000 * 60 * 7);
                    $("#stopper").html("<i class='iconfont icon-play' aria-hidden='true'></i>");
                    isClockWorking = 0;//to stop
                    ipc.send("tray-image-change", "stop");
                    if (store.get("alarmtip") !== false) pausingTimeout = setInterval(function () {
                        ipc.send("notify", {
                            title: i18n.__('alarm-for-not-using-wnr-dialog-box-title'),
                            msg: i18n.__('alarm-for-not-using-wnr-dialog-box-content')
                        });
                    }, 600000);
                } else {
                    if (pauseInt != null)
                        window.clearTimeout(pauseInt);
                    if (methodFromStart !== 3) {
                        if (method === 1) startTime = new Date().getTime() - (workTime - hours * 3600000 - minutes * 60000 - seconds * 1000);
                        else startTime = new Date().getTime() - (restTime - hours * 3600000 - minutes * 60000 - seconds * 1000);
                        int = self.setInterval(clock, 250);
                    } else {
                        startTime = new Date().getTime() - hours * 3600000 - minutes * 60000 - seconds * 1000;
                        int = self.setInterval(positive, 250);
                    }
                    $("#stopper").html("<i class='iconfont icon-pause' aria-hidden='true'></i>");
                    isClockWorking = 1;//to restart
                    ipc.send("tray-image-change", "start");
                    if (pausingTimeout) window.clearInterval(pausingTimeout);
                }
            }

            function warningGiver(isend) {
                if (isFirstTimeOnlyRest) {
                    isFirstTimeOnlyRest = false;
                    if (restTimeFocused) ipc.send("only-rest-fullscreen");
                    return;
                }
                if ((method === 1 && workTime > 60000) || (method === 2 && restTime > 60000)) moreThanOneMin = 1;
                else moreThanOneMin = 0;//if less than 1 min, prevent less than 1min warning
                lastRecordedMinutesLeft = -1, lastRecordedHoursLeft = -1, lastRecordedProgress = 0.00;
                canStop = false;
                timingData.set("last-recorded-minutes-left", -1);
                timingData.set("last-recorded-hours-left", -1);
                let player = document.createElement('audio');
                amplifyMedia(player, amplifierList[store.get("sound")]);
                if (isend === 1) {
                    let workSelection = store.has("work-time-end-sound") ? store.get("work-time-end-sound")
                        : (store.has("time-end-sound") ? store.get("time-end-sound") : "tick");
                    if (workSelection !== i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\" + workSelection + ".mp3");
                    else {
                        let customWork = store.get("custom-work-time-end-sound");
                        if (!customWork || customWork === "")
                            customWork = path.join(__dirname, "\\res\\sound\\tick.mp3");
                        player.src = customWork;
                    }
                } else if (isend === 2) {
                    let restSelection = store.has("rest-time-end-sound") ? store.get("rest-time-end-sound")
                        : (store.has("time-end-sound") ? store.get("time-end-sound") : "tick");
                    if (restSelection !== i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\" + restSelection + ".mp3");
                    else {
                        let customRest = store.get("custom-rest-time-end-sound");
                        if (!customRest || customRest === "")
                            customRest = store.get("custom-work-time-end-sound");
                        if (!customRest || customRest === "")
                            customRest = path.join(__dirname, "\\res\\sound\\tick.mp3");
                        player.src = customRest;
                    }
                } else {
                    if (store.get("all-end-sound") !== i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\" + (store.has("all-end-sound") ? store.get("all-end-sound") : "piano") + ".mp3");
                    else if ((!store.has("custom-all-time-end-sound") || store.get("custom-all-time-end-sound") === "")
                            && store.get("all-end-sound") === i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\piano.mp3");
                    else
                        player.src = store.get("custom-all-time-end-sound");
                }
                player.loop = !store.get("sound-play-once");
                player.play();
                if ((method === 2 && store.get("no-check-work-time-end")) || (method === 1 && store.get("no-check-rest-time-end"))) {
                    $("#skipper-container").css("display", "none");
                    $("#skipper-space").css("display", "none");
                    setTimeout(function () {
                        player.loop = false;
                        if (!isFeatureDisabled(store.get('disable-skipping'), method)) {
                            $("#skipper-container").css("display", "inline");
                            if ((!store.get("islocked")) && (!isFeatureDisabled(store.get('disable-backing'), method))) $("#skipper-space").css("display", "inline");
                            else $("#skipper-space").css("display", "none");
                        } else {
                            $("#skipper-container").css("display", "none");
                            $("#skipper-space").css("display", "none");
                        }
                    }, 3000);
                    canStop = true;
                } else {
                    setTimeout(function () {
                        if (isClockWorking) stopper();
                    }, 1000);
                    setTimeout(function () {
                        player.loop = false;
                    }, 20000);
                }
                if (isend === 0) ipc.send('warning-giver-all-task-end');
                else {
                    if (isend === 1) ipc.send('warning-giver-workend');
                    else ipc.send('warning-giver-restend');
                    if ((method === 2 && store.get("no-check-work-time-end")) || (method === 1 && store.get("no-check-rest-time-end")))
                        if (!isClockWorking) stopperTimeout = setTimeout(stopper, 500);
                    ipc.once('warning-closed', function () {
                        player.loop = false;
                        player.pause();

                        if (!isClockWorking) stopper();
                        if (!isFeatureDisabled(store.get('disable-skipping'), method)) {
                            $("#skipper-container").css("display", "inline");
                            if ((!store.get("islocked")) && (!isFeatureDisabled(store.get('disable-backing'), method))) $("#skipper-space").css("display", "inline");
                            else $("#skipper-space").css("display", "none");
                        } else {
                            $("#skipper-container").css("display", "none");
                            $("#skipper-space").css("display", "none");
                        }
                        canStop = true;
                    });
                }
            }

            function themeChanger() {
                if (method === 1) {
                    isBothSkipperAndBackerEnabled = true;
                    if (isFeatureDisabled(store.get('disable-pausing'), 2)) {
                        $("#stopper").css("display", "none");
                    } else $("#stopper").css("display", "block");
                    if (isFeatureDisabled(store.get('disable-skipping'), 2)) {
                        $("#skipper-container").css("display", "none");
                        isBothSkipperAndBackerEnabled = false;
                    } else {
                        $("#skipper-container").css("display", "inline");
                    }
                    if (isFeatureDisabled(store.get('disable-backing'), 2)) {
                        $("#back-index").css("display", "none");
                        $("#mistake-button").addClass("disabled").css("pointer-events", "none").css("opacity", "0.5");
                        isBothSkipperAndBackerEnabled = false;
                    } else {
                        $("#back-index").css("display", "inline-block");
                        $("#mistake-button").removeClass("disabled").css("pointer-events", "auto").css("opacity", "1");
                    }
                    
                    // Handle time adjustment dropdown visibility for rest mode
                    const isRestFocusedMode = restTimeFocused;
                    const isLooseMode = methodFromStart === 3;
                    
                    if (isFeatureDisabled(store.get('disable-time-adjust'), 2) || (isRestFocusedMode && !isLooseMode)) {
                        $("#time-adjust-dropdown").hide();
                    } else {
                        $("#time-adjust-dropdown").show();
                    }
                    if (isBothSkipperAndBackerEnabled) $("#skipper-space").css("display", "inline");
                    else $("#skipper-space").css("display", "none");
                    $("#only-one").css("display", "none");
                    $(".work").addClass(isOnlyRest ? "onlyRest" : "rest");//onlyRest mode now has specific color
                    $(".work").removeClass("work");
                    if (isOnlyRest) $("#work-rest").text(i18n.__('onlyrest'));
                    else if (isLongRest) $("#work-rest").html(i18n.__('longresting'));
                    else $("#work-rest").html(i18n.__('resting'));
                    $bottomBar.css("background-color", isOnlyRest ? newOnlyRestColor + "99" : newRestColor + "99");
                    method = 2;
                    timingData.set("last-recorded-state.method", 2);
                    if (restTimeFocused === true) {
                        if (styleCache.get("is-shadowless") !== true) $("html").css("border", "none");
                        $bottomBar.css("background-color", isOnlyRest ? newOnlyRestColor + "19" : newRestColor + "19");
                        $bottomBar.css("height", "100%");
                        $("#controller").css("display", "none");
                        $("#more-options").css("display", "none");
                        $timeBarMacOS.addClass("time-bar-focus");
                        $timeBar.addClass("time-bar-focus");
                        $nowTiming.addClass("display-1");
                        $nowTiming.removeClass("h1");
                        if (store.get("fullscreen-experience") !== true) {
                            $("#fullscreen-experience-tip").css("display", "inline-block");
                            store.set("fullscreen-experience", true);
                        }
                    } else {
                        if (hasFloating) {
                            floatingOpener();
                                                    if (!isFeatureDisabled(store.get('disable-skipping'), 2)) {
                            $("#skipper-container").css("display", "inline");
                        }
                        }

                        if (workTimeFocused === true) {
                            if (styleCache.get('isdark')) $('html').css("border", "#ffffff33 1px solid");
                            $bottomBar.css("height", "1.9%");
                            if (systemName !== "darwin") $("#controller").css("display", "block");
                            $("#more-options").css("display", "block");
                            $(".time-bar-focus").removeClass("time-bar-focus");
                            $nowTiming.removeClass("display-1");
                            $nowTiming.addClass("h1");
                            $("#fullscreen-experience-tip").css("display", "none");
                        }
                    }
                    warningGiver(1);
                } else {
                    if (isFeatureDisabled(store.get('disable-pausing'), 1)) {
                        $("#stopper").css("display", "none");
                    } else $("#stopper").css("display", "block");
                    if (isFeatureDisabled(store.get('disable-skipping'), 1)) {
                        $("#skipper-container").css("display", "none");
                    } else {
                        $("#skipper-container").css("display", "inline");
                    }
                    if (isFeatureDisabled(store.get('disable-backing'), 1)) {
                        $("#back-index").css("display", "none");
                        $("#mistake-button").addClass("disabled").css("pointer-events", "none").css("opacity", "0.5");
                    } else {
                        $("#back-index").css("display", "inline-block");
                        $("#mistake-button").removeClass("disabled").css("pointer-events", "auto").css("opacity", "1");
                    }
                    
                    // Handle time adjustment dropdown visibility for work mode
                    const isWorkFocusedMode = workTimeFocused;
                    const isLooseMode = methodFromStart === 3;
                    
                    if (isFeatureDisabled(store.get('disable-time-adjust'), 1) || (isWorkFocusedMode && !isLooseMode)) {
                        $("#time-adjust-dropdown").hide();
                    } else {
                        $("#time-adjust-dropdown").show();
                    }
                    $("#only-one").css("display", "none");
                    $(".rest").addClass("work");
                    $(".rest").removeClass("rest");
                    $("#work-rest").html(i18n.__('working'));
                    $bottomBar.css("background-color", newWorkColor + "99");
                    method = 1;
                    timingData.set("last-recorded-state.method", 1);
                    if (systemName !== "darwin") $("#controller").css("display", "block");
                    if (workTimeFocused === true) {
                        if (styleCache.get("is-shadowless") !== true) $("html").css("border", "none");
                        $bottomBar.css("background-color", newWorkColor + "19");
                        $bottomBar.css("height", "100%");
                        $("#controller").css("display", "none");
                        $("#more-options").css("display", "none");
                        $timeBar.addClass("time-bar-focus");
                        $timeBarMacOS.addClass("time-bar-focus");
                        $nowTiming.addClass("display-1");
                        $nowTiming.removeClass("h1");
                        if (store.get("fullscreen-experience") !== true) {
                            $("#fullscreen-experience-tip").css("display", "inline-block");
                            store.set("fullscreen-experience", true);
                        }
                    } else {
                        if (hasFloating) {
                            floatingOpener();
                            if (!isFeatureDisabled(store.get('disable-skipping'), 1)) {
                                $("#skipper-container").css("display", "inline");
                            }
                        }

                        if (restTimeFocused === true) {
                            if (styleCache.get('isdark')) $('html').css("border", "#ffffff33 1px solid");
                            $bottomBar.css("height", "1.9%");
                            $("#more-options").css("display", "block");
                            $(".time-bar-focus").removeClass("time-bar-focus");
                            $nowTiming.removeClass("display-1");
                            $nowTiming.addClass("h1");
                            $("#fullscreen-experience-tip").css("display", "none");
                        }
                    }
                    if (shouldNap) {
                        lastNapSecond = workTime / 1000;
                    }
                    warningGiver(2);
                }
            }

            function ender() {
                canStop = false;
                call('floating-destroy');
                window.clearInterval(int);
                $("#time-cnt").css("display", "none");
                $("#stop-count").css("display", "none");
                $("#only-one").css("display", "none");
                $("#fullscreen-experience-tip").css("display", "none");
                $("#work-rest").text("");
                $nowTiming.text(i18n.__('ended'));
                isClockWorking = 0;
                $("#stopper").text("");
                $bottomBar.css("width", "0%");
                ipc.send("progress-bar-set", {
                    progress: -1,
                    remain: 0,
                    positive: false
                });
                if (systemName !== "darwin") $("#controller").css("display", "block");
                $("#backer").css("display", "block");
                $("#more-options").css("display", "none");
                if (styleCache.get('isdark')) $('html').css("border", "#ffffff33 1px solid");
                if (restTimeFocused === true) {
                    $(".time-bar-focus").removeClass("time-bar-focus");
                    $nowTiming.removeClass("display-1");
                    $nowTiming.addClass("h1");
                }
                warningGiver(0);
            }
            function adjustTime(minutes){
                if (!window.isClockWorking) return;
                const ms = minutes * 60 * 1000;                
                // adjust time according to mode
                if (method === 1) {
                    // working mode: adjust work time
                    workTime = workTime + ms;
                } else {
                    // rest mode: adjust rest time
                    restTime = restTime + ms;
                }
                lastRecordedProgress = -1;
                
                // Check if timer should end immediately after adjustment
                if (workTime <= 0 || restTime <= 0) {
                    // End the timer immediately
                    ender();
                } else {
                    // Ensure minimum time is 10 seconds if not ending
                    if (method === 1) {
                        workTime = Math.max(10000, workTime);
                    } else {
                        restTime = Math.max(10000, restTime);
                    }
                }
            }

            function shouldLongBreak() {
                if (longBreakFrequence === -1) return times === loop * 2 - 1 && loop > 2;
                else return ((times + 1) % (longBreakFrequence * 2)) === 0; // new long break optimization
            }

            function skipper() {
                workTime= originalWorkTime;
                canOpenFloating = true;
                hasSoonFinishTipPosted = false;
                if (pausingTimeout) window.clearInterval(pausingTimeout);
                if (continueLoop && method === 2) {
                    //if continueloop then only play once and no other thing
                    let player = document.createElement('audio');
                    amplifyMedia(player, amplifierList[store.get("sound")]);
                    if (store.get("time-end-sound") !== i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\" + (store.has("time-end-sound") ? store.get("time-end-sound") : "tick") + ".mp3");
                    else if ((!store.has("custom-work-time-end-sound") || store.get("custom-work-time-end-sound") === "")
                            && store.get("time-end-sound") === i18n.__("custom"))
                        player.src = path.join(__dirname, "\\res\\sound\\tick.mp3");
                    else
                        player.src = store.get("custom-work-time-end-sound");
                    player.loop = false;
                    player.play();
                } else {
                    if (methodFromStart !== 3) {
                        $("#skipper-container").css("display", "none");
                        $("#skipper-space").css("display", "none");
                        $("#passcode-rechecker").css("display", "none");
                    }
                    times++;
                    $("#time-cnt").text(Math.ceil((times + 1) / 2) + ' / ' + loop + ' ' + i18n.__('times'));
                }
                if (isClockWorking) {
                    if (continueLoop && method === 2) {
                        startTime = new Date().getTime();
                    } else if (times < loop * 2 || loop === 0) {
                        startTime = new Date().getTime();
                        if (shouldLongBreak()) {//long break
                            let long = longBreakGetter();
                            restTime = originalRestTime + long * 60000;
                            isLongRest = true;
                        } else {
                            restTime = originalRestTime;
                            isLongRest = false;
                        }
                        if (methodFromStart !== 3) themeChanger();
                    } else if (times === loop * 2) {
                        ender();
                    }
                    if (methodFromStart === 3 && hasFloating) {
                        floatingOpener();
                        if (!isFeatureDisabled(store.get('disable-skipping'), 1)) {
                            $("#skipper-container").css("display", "inline");
                        }
                    }
                } else {
                    stopper();
                    setTimeout(function () {
                        if (continueLoop && method === 2) {
                            startTime = new Date().getTime();
                        } else if (times < loop * 2 || loop === 0) {
                            startTime = new Date().getTime();
                            if (shouldLongBreak()) {//long break
                                let long = longBreakGetter();
                                restTime = restTime + long * 60000;
                                isLongRest = true;
                            } else {
                                restTime = originalRestTime;
                                
                                isLongRest = false;
                            }
                            if (methodFromStart !== 3) themeChanger();
                        } else if (times === loop * 2) {
                            ender();
                        }
                        if (methodFromStart === 3 && hasFloating) {
                            floatingOpener();
                            if (!isFeatureDisabled(store.get('disable-skipping'), 1)) {
                                $("#skipper-container").css("display", "inline");
                            }
                        }
                    }, 250);
                }
            }

            function positionCover(str) {//cover the positions of '0'
                let num;
                str >= 10 ? num = str : num = "0" + str;
                return num;
            }

            //positive timer
            function positive() {
                nowTime = new Date();
                tempVariable = nowTime.getTime();
                seconds = Math.floor((tempVariable - startTime) / 1000);

                if (seconds - lastNapSecond > napInterval) {
                    lastNapSecond = seconds;
                    ipc.send("notify", { title: i18n.__('should-nap-now'), msg: i18n.__('should-nap-now-msg') });
                }

                hours = Math.floor(seconds / 3600);
                minutes = Math.floor((seconds - hours * 3600) / 60);

                if (hasFloating) ipc.send("floating-conversation", {
                    topic: "time-left",
                    val: seconds / 60,
                    percentage: 0,
                    method: 3,
                    isWorking: isClockWorking
                });

                if (lastTimePositiveMinutes !== minutes) {
                    lastTimePositiveMinutes = minutes;
                    ipc.send("progress-bar-set", {
                        progress: -1,
                        remain: hours * 60 + minutes,
                        positive: true
                    });
                    ipc.send("tray-time-set", {
                        h: hours,
                        min: minutes,
                        positive: true
                    });
                }

                seconds -= hours * 3600 + minutes * 60;
                $nowTiming.text(hours + h + minutes + min + seconds + s);

                timeGot = positionCover(nowTime.getHours()) + ":" + positionCover(nowTime.getMinutes());
                if (systemName !== "darwin") $timeBar.html(timeGot);
                else $timeBarMacOS.html(timeGot);//display the current time
            }

            //countdown timer
            function clock() {
                nowTime = new Date();
                tempVariable = nowTime.getTime();
                if (method === 1) seconds = Math.floor((workTime - tempVariable + startTime -
                        (times === 0 ? firstTimeDiscount : 0)) / 1000);
                else seconds = Math.floor((restTime - tempVariable + startTime) / 1000);
                if (seconds > 0) {
                    if (method === 1) {
                        progress = ((seconds / workTime) * 1000).toFixed(2);
                    } else {
                        progress = ((seconds / restTime) * 1000).toFixed(2);
                    }

                    if (shouldNap && method === 1) {
                        if (lastNapSecond - seconds > napInterval) {
                            lastNapSecond = seconds;
                            ipc.send("notify", {
                                title: i18n.__('should-nap-now'),
                                msg: i18n.__('should-nap-now-msg')
                            });
                        }
                    }

                    hours = Math.floor(seconds / 3600);
                    minutes = Math.floor((seconds - hours * 3600) / 60);
                    remainMinutes = hours * 60 + minutes;

                    if (hours === 0 && minutes === 0 && (lastRecordedSecond !== seconds)) {
                        lastRecordedSecond = seconds;
                        if (hasFloating) ipc.send("floating-conversation", {
                            topic: "time-left",
                            val: seconds / 60,
                            percentage: naturalProgress,
                            method: method,
                            isWorking: isClockWorking
                        });
                    }

                    if (remainMinutes !== lastRecordedRemain) {
                        lastRecordedRemain = remainMinutes;
                        ipc.send("progress-bar-set", {
                            progress: progress,
                            remain: minutes + hours * 60,
                            positive: false
                        });
                        ipc.send("tray-time-set", {
                            h: hours,
                            min: minutes,
                            positive: false
                        });
                    }

                    if (progress !== lastRecordedProgress) {
                        lastRecordedProgress = progress;
                        naturalProgress = Math.round(lastRecordedProgress * 100);
                        ipc.send("progress-bar-set", {
                            progress: progress,
                            remain: minutes + hours * 60,
                            positive: false
                        });
                        if (hasFloating) ipc.send("floating-conversation", {
                            topic: "time-left",
                            val: seconds / 60,
                            percentage: naturalProgress,
                            method: method,
                            isWorking: isClockWorking
                        });
                        $bottomBar.css("width", naturalProgress + "%");
                    }

                    seconds -= hours * 3600 + minutes * 60;
                    $nowTiming.text(hours + h + minutes + min + seconds + s);

                    if (hours === 0 && minutes === 0 && seconds < 10) canOpenFloating = false;

                    timeGot = positionCover(nowTime.getHours()) + ":" + positionCover(nowTime.getMinutes());
                    if (systemName !== "darwin") $timeBar.html(timeGot);
                    else $timeBarMacOS.html(timeGot);//display the current time

                    if (lastRecordedHoursLeft !== hours) {
                        lastRecordedHoursLeft = hours;
                        timingData.set("last-recorded-hours-left", lastRecordedHoursLeft);
                    }
                    if (lastRecordedMinutesLeft !== minutes) {
                        lastRecordedMinutesLeft = minutes;
                        timingData.set("last-recorded-minutes-left", lastRecordedMinutesLeft + 1);
                    }

                    if (minutes === 0 && hours === 0) {
                        if (moreThanOneMin) {
                            if (store.get("onemintip") !== false) {
                                if ((method === 1 && workTimeFocused === false) || (method === 2 && restTimeFocused === false))
                                    ipc.send("only-one-min-left");
                                else {
                                    let player = document.createElement('audio');
                                    amplifyMedia(player, amplifierList[store.get("sound") * 1.4]);
                                    player.src = path.join(__dirname, "\\res\\sound\\only-one.wav");
                                    player.loop = false;
                                    player.play();
                                    $("#only-one").css("display", "block");
                                }
                            }
                            moreThanOneMin = 0;
                        } else if (seconds <= soonFinishTipMethod && !hasSoonFinishTipPosted) {
                            if (!((method === 1 && workTimeFocused) || (method === 2 && restTimeFocused)))
                                ipc.send("notify", {
                                    title: i18n.__('soon-finish-tip-notification-title-1') + soonFinishTipMethod + i18n.__('soon-finish-tip-notification-title-2'),
                                    msg: i18n.__('soon-finish-tip-notification-msg')
                                });
                            hasSoonFinishTipPosted = true;
                        }
                    }
                } else {
                    skipper();
                }
            }
        }
    </script>
    <script src="renderer.js"></script>
    <script src="theme.js"></script>
</body>

</html>